package com.we.flink.dcapiuserlabel.table;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;

public class TableBaOprCmsOfflineUserLabel {
    public static final String KFKTABLE = "fdm.rrd03_ba_opr_cms_offline_user_label_v2";
    public static final String QUERYSQL =
            "select uid, "
                    + "ifnull(is_opr_strategy_reach_this_month,1) as is_opr_strategy_reach_this_month, "
                    + "ifnull(sms_reach_time,0) as sms_reach_time, "
                    + "ifnull(coupon_reach_time,0) as coupon_reach_time, "
                    + "ifnull(push_reach_time,0) as push_reach_time, "
                    + "ifnull(app_msg_reach_time,0) as app_msg_reach_time, "
                    + "ifnull(call_cnt_aft_last_borrow_time,0) as call_cnt_af_last_borrow_time, "
                    + "ifnull(call_cnt_aft_last_audit_suc_time,0) as call_cnt_af_last_audit_time "
                    + "from "
                    + KFKTABLE
                    + " where uid is not null ";

    public TableBaOprCmsOfflineUserLabel() {}

    public TableBaOprCmsOfflineUserLabel(
            Long uid,
            int is_opr_strategy_reach_this_month,
            int sms_reach_time,
            int coupon_reach_time,
            int push_reach_time,
            int app_msg_reach_time,
            int call_cnt_af_last_borrow_time,
            int call_cnt_af_last_audit_time) {
        this.uid = uid;
        this.is_opr_strategy_reach_this_month = is_opr_strategy_reach_this_month;
        this.sms_reach_time = sms_reach_time;
        this.coupon_reach_time = coupon_reach_time;
        this.push_reach_time = push_reach_time;
        this.app_msg_reach_time = app_msg_reach_time;
        this.call_cnt_af_last_borrow_time = call_cnt_af_last_borrow_time;
        this.call_cnt_af_last_audit_time = call_cnt_af_last_audit_time;
    }

    public static TableBaOprCmsOfflineUserLabel init(long uid) {
        return new TableBaOprCmsOfflineUserLabel(uid, 1, 0, 0, 0, 0, -1, -1);
    }

    public Long uid;
    public int is_opr_strategy_reach_this_month;
    public int sms_reach_time;
    public int coupon_reach_time;
    public int push_reach_time;
    public int app_msg_reach_time;
    public int call_cnt_af_last_borrow_time;
    public int call_cnt_af_last_audit_time;

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableBaOprCmsOfflineUserLabel.QUERYSQL);
        DataStream<Tuple2<Boolean, TableBaOprCmsOfflineUserLabel>> inputStream =
                tableEnv.toRetractStream(tableRes, TableBaOprCmsOfflineUserLabel.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<
                                        Tuple2<Boolean, TableBaOprCmsOfflineUserLabel>>() {
                                    @Override
                                    public boolean filter(
                                            Tuple2<Boolean, TableBaOprCmsOfflineUserLabel> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .uid("tableBaOprCmsOfflineUserLabel-filter")
                        .keyBy(
                                new KeySelector<
                                        Tuple2<Boolean, TableBaOprCmsOfflineUserLabel>, Long>() {
                                    @Override
                                    public Long getKey(
                                            Tuple2<Boolean, TableBaOprCmsOfflineUserLabel> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableBaOprCmsOfflineUserLabel>, String>() {
                                    ValueState<TableBaOprCmsOfflineUserLabel>
                                            baOprOfflineUserLabelVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        baOprOfflineUserLabelVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TableBaOprCmsOfflineUserLabel>(
                                                                        "BaOprOfflineUserLabelVState",
                                                                        TableBaOprCmsOfflineUserLabel
                                                                                .class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableBaOprCmsOfflineUserLabel> value,
                                            Collector<String> out)
                                            throws Exception {
                                        Long uid = value.f1.getUid();
                                        int is_opr_strategy_reach_this_month =
                                                value.f1.getIs_opr_strategy_reach_this_month();
                                        int sms_reach_time = value.f1.getSms_reach_time();
                                        int coupon_reach_time = value.f1.getCoupon_reach_time();
                                        int push_reach_time = value.f1.getPush_reach_time();
                                        int app_msg_reach_time = value.f1.getApp_msg_reach_time();
                                        int call_cnt_af_last_borrow_time =
                                                value.f1.getCall_cnt_af_last_borrow_time();
                                        int call_cnt_af_last_audit_time =
                                                value.f1.getCall_cnt_af_last_audit_time();
                                        TableBaOprCmsOfflineUserLabel tmpState =
                                                baOprOfflineUserLabelVState.value();
                                        TableBaOprCmsOfflineUserLabel curState =
                                                tmpState == null
                                                        ? TableBaOprCmsOfflineUserLabel.init(uid)
                                                        : tmpState;
                                        JSONObject jsonObject = new JSONObject();
                                        jsonObject.put("uid", uid);

                                        /** use state value 0 to filter value of "null" */
                                        if (curState.getIs_opr_strategy_reach_this_month()
                                                != is_opr_strategy_reach_this_month) {
                                            curState.is_opr_strategy_reach_this_month =
                                                    is_opr_strategy_reach_this_month;
                                            jsonObject.put(
                                                    "is_opr_strategy_reach_this_month",
                                                    is_opr_strategy_reach_this_month);
                                        }

                                        if (curState.getSms_reach_time() != sms_reach_time) {
                                            curState.sms_reach_time = sms_reach_time;
                                            jsonObject.put("sms_reach_time", sms_reach_time);
                                        }
                                        if (curState.getCoupon_reach_time() != coupon_reach_time) {
                                            curState.coupon_reach_time = coupon_reach_time;
                                            jsonObject.put("coupon_reach_time", coupon_reach_time);
                                        }
                                        if (curState.getPush_reach_time() != push_reach_time) {
                                            curState.push_reach_time = push_reach_time;
                                            jsonObject.put("push_reach_time", push_reach_time);
                                        }
                                        if (curState.getApp_msg_reach_time()
                                                != app_msg_reach_time) {
                                            curState.app_msg_reach_time = app_msg_reach_time;
                                            jsonObject.put(
                                                    "app_msg_reach_time", app_msg_reach_time);
                                        }
                                        if (curState.getCall_cnt_af_last_borrow_time()
                                                != call_cnt_af_last_borrow_time) {
                                            curState.call_cnt_af_last_borrow_time =
                                                    call_cnt_af_last_borrow_time;
                                            jsonObject.put(
                                                    "call_cnt_af_last_borrow_time",
                                                    call_cnt_af_last_borrow_time);
                                        }
                                        if (curState.getCall_cnt_af_last_audit_time()
                                                != call_cnt_af_last_audit_time) {
                                            curState.call_cnt_af_last_audit_time =
                                                    call_cnt_af_last_audit_time;
                                            jsonObject.put(
                                                    "call_cnt_af_last_audit_time",
                                                    call_cnt_af_last_audit_time);
                                        }

                                        if (jsonObject.size() > 1) {
                                            baOprOfflineUserLabelVState.update(curState);
                                            out.collect(jsonObject.toString());
                                        }
                                    }
                                });

        return out;
    }

    public Long getUid() {
        return uid;
    }

    public int getIs_opr_strategy_reach_this_month() {
        return is_opr_strategy_reach_this_month;
    }

    public int getSms_reach_time() {
        return sms_reach_time;
    }

    public int getCoupon_reach_time() {
        return coupon_reach_time;
    }

    public int getPush_reach_time() {
        return push_reach_time;
    }

    public int getApp_msg_reach_time() {
        return app_msg_reach_time;
    }

    public int getCall_cnt_af_last_borrow_time() {
        return call_cnt_af_last_borrow_time;
    }

    public int getCall_cnt_af_last_audit_time() {
        return call_cnt_af_last_audit_time;
    }
}
